//
//  LMDetailsController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/14.
//  Copyright © 2016年 楼 眉. All rights reserved.
//

#import "LMDetailsController.h"
#import <YYWebImage.h>
#import <SDCycleScrollView.h>

static CGFloat const DetailsSpace = 10;

@interface LMDetailsController ()<SDCycleScrollViewDelegate>
@property (nonatomic,weak)  UIScrollView *bgScrollView;
@property (nonatomic,weak)  UIScrollView *topScrollView;
@property (nonatomic,weak)  UIImageView *middlePriceView;
@property (nonatomic,weak)  UILabel *nameLabel;
@property (nonatomic,weak)  UILabel *actPriceLabel;
@property (nonatomic,weak)  UILabel *originalPriceLabel;
@property (nonatomic,weak)  UIView *lineView;
@property (nonatomic,weak)  UIImageView *firstImageView;
@property (nonatomic,weak)  UIImageView *secondImageView;
@property (nonatomic,weak)  SDCycleScrollView *WheelScrollView;
@property (nonatomic,strong)  UIImageView *parameterView;
@end

@implementation LMDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitle];
    [self setupUi];
}
- (void)setupUi
{
    UIScrollView *bgScrollView = [[UIScrollView alloc]init];
    bgScrollView.frame = self.view.bounds;
    bgScrollView.alwaysBounceVertical = YES;
    self.bgScrollView = bgScrollView;
    [self.view addSubview:bgScrollView];

    SDCycleScrollView *WheelScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3) delegate:self placeholderImage:nil];
    self.WheelScrollView = WheelScrollView;
    WheelScrollView.imageURLStringsGroup = nil;
    WheelScrollView.autoScrollTimeInterval = 3;
    WheelScrollView.currentPageDotColor = LMRandomColor;
    WheelScrollView.pageDotColor = LMRandomColor;
    [bgScrollView addSubview:WheelScrollView];
    
    UIImageView *middlePriceView = [[UIImageView alloc]init];
    middlePriceView.frame = CGRectMake(0, CGRectGetMaxY(WheelScrollView.frame) + DetailsSpace, SCREEN_WIDTH,200);
    self.middlePriceView = middlePriceView;
//    middlePriceView.backgroundColor = LMRandomColor;
    [bgScrollView addSubview:middlePriceView];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.font = [UIFont systemFontOfSize:20];
    nameLabel.textColor = RGBCOLOR(100, 100, 100);
    nameLabel.numberOfLines = 0;
    self.nameLabel = nameLabel;
    [middlePriceView addSubview:nameLabel];
    
    UILabel *actPriceLabel = [[UILabel alloc]init];
    actPriceLabel.font = [UIFont systemFontOfSize:18];
    actPriceLabel.numberOfLines = 0;
    actPriceLabel.textColor = [UIColor redColor];
    actPriceLabel.textAlignment = NSTextAlignmentCenter;
    self.actPriceLabel = actPriceLabel;
    [middlePriceView addSubview:actPriceLabel];
    
    UILabel *originalPriceLabel = [[UILabel alloc]init];
    originalPriceLabel.font = [UIFont systemFontOfSize:16];
    originalPriceLabel.numberOfLines = 0;
    originalPriceLabel.textColor = [UIColor lightGrayColor];
    originalPriceLabel.textAlignment = NSTextAlignmentCenter;
    self.originalPriceLabel = originalPriceLabel;
    [middlePriceView addSubview:originalPriceLabel];
    
    UIView *lineVIew = [[UIView alloc]init];
    self.lineView = lineVIew;
    lineVIew.backgroundColor = [UIColor lightGrayColor];
    [originalPriceLabel addSubview:lineVIew];
    
    UIImageView *parameterView = [[UIImageView alloc]init];
    self.parameterView = parameterView;
    [bgScrollView addSubview:parameterView];
    
    UILabel *parameterTitleLabel = [[UILabel alloc]init];
    parameterTitleLabel.text = @"参数详情";
    parameterTitleLabel.font = [UIFont boldSystemFontOfSize:18];
    parameterTitleLabel.frame = CGRectMake(DetailsSpace, 0, SCREEN_WIDTH, 33);
    [parameterView addSubview:parameterTitleLabel];
}
- (void)setHome:(AVObject *)home
{
    _home = home;
    
    NSArray *urls = [home objectForKey:@"url"];
    self.WheelScrollView.imageURLStringsGroup = urls;
    
    NSDictionary *nameLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
    NSString *commodity = [home objectForKey:@"commodity"];
    self.nameLabel.text = commodity;
    CGSize nameLabelSize = [commodity boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - DetailsSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameLabelAttri context:nil].size;
    self.nameLabel.frame = (CGRect){{DetailsSpace,0},nameLabelSize};
    
    NSDictionary *newPriceLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:18]};
    NSString *temp = [home objectForKey:@"newPrice"];
    NSString *newPrice = [NSString stringWithFormat:@"活动价:  %@",temp];
    self.actPriceLabel.text = newPrice;
    CGSize newPriceLabelSize = [newPrice boundingRectWithSize:CGSizeMake(SCREEN_WIDTH / 2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:newPriceLabelAttri context:nil].size;
    self.actPriceLabel.frame = (CGRect){{DetailsSpace,CGRectGetMaxY(self.nameLabel.frame) + DetailsSpace},newPriceLabelSize};
    
    NSDictionary *originalPriceLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
    NSString *originalTemp = [home objectForKey:@"originalPrice"];
    NSString *originalPrice = [NSString stringWithFormat:@"原价:  %@",originalTemp];
    self.originalPriceLabel.text = originalPrice;
    CGSize originalPriceLabelSize = [originalPrice boundingRectWithSize:CGSizeMake(SCREEN_WIDTH / 2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:originalPriceLabelAttri context:nil].size;
    self.originalPriceLabel.frame = (CGRect){{CGRectGetMaxX(self.actPriceLabel.frame) +DetailsSpace,CGRectGetMaxY(self.actPriceLabel.frame) - originalPriceLabelSize.height},originalPriceLabelSize};
    
    self.lineView.frame = CGRectMake(0,self.originalPriceLabel.height / 2, self.originalPriceLabel.width, 1);
    
    UIView *lastView = [self.middlePriceView.subviews lastObject];
    self.middlePriceView.height = CGRectGetMaxY(lastView.frame);
    
    NSDictionary *valueAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    NSDictionary *dict = [[home objectForKey:@"parameter"] firstObject];
    
    for (int index = 0; index < dict.allKeys.count; index ++) {
        UIView *paraLastView = [self.parameterView.subviews lastObject];
        UIImageView *labelBgView = [[UIImageView alloc]init];
        labelBgView.frame = CGRectMake(0, CGRectGetMaxY(paraLastView.frame) + 10, SCREEN_WIDTH, 0);
        
        UILabel *valueLabel = [[UILabel alloc]init];
        valueLabel.font = [UIFont systemFontOfSize:15];
        valueLabel.numberOfLines = 0;
        valueLabel.textColor = RGBCOLOR(100, 100, 100);
        valueLabel.text = dict.allValues[index];
        CGSize valueLabelSize = [valueLabel.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 100 - DetailsSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:valueAttri context:nil].size;
        valueLabel.x = 100 + DetailsSpace;
        valueLabel.y = 0;
        valueLabel.size = valueLabelSize;
        [labelBgView addSubview:valueLabel];
        UILabel *keyLabel = [[UILabel alloc]init];
        keyLabel.text = dict.allKeys[index];
        keyLabel.font = [UIFont systemFontOfSize:15];
        keyLabel.textColor = RGBCOLOR(100, 100, 100);
        keyLabel.frame = CGRectMake(DetailsSpace, 0, 100, valueLabel.height);
        [labelBgView addSubview:keyLabel];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.frame = CGRectMake(10, CGRectGetMaxY(valueLabel.frame) + 5, SCREEN_WIDTH - 20, 1);
        lineView.backgroundColor = RGBCOLOR(240, 240, 240);
        [labelBgView addSubview:lineView];
        labelBgView.height = CGRectGetMaxY(lineView.frame);

        
        [self.parameterView addSubview:labelBgView];
    }
    UIView *myView = [self.parameterView.subviews lastObject];
    self.parameterView.frame = CGRectMake(0, CGRectGetMaxY(self.middlePriceView.frame) + DetailsSpace, SCREEN_WIDTH, CGRectGetMaxY(myView.frame));
    
    UILabel *addressBtn = [[UILabel alloc]init];
//    UIImageView *addLeftImageView = [[UIImageView alloc]init];
//    addLeftImageView.frame = CGRectMake(0, 0, 33, 33);
//    addLeftImageView.image = [UIImage imageNamed:@"address_highlight"];
//    [addressBtn addSubview:addLeftImageView];
    addressBtn.font = [UIFont systemFontOfSize:15.0];
    NSString *addString = [home objectForKey:@"shopName"];
    NSString *addressString = [NSString stringWithFormat:@"地址：%@",addString];
    addressBtn.text = addressString;
    addressBtn.textAlignment = NSTextAlignmentLeft;
    addressBtn.frame = CGRectMake(DetailsSpace *kUnit, CGRectGetMaxY(self.parameterView.frame) + DetailsSpace, 210, 33);
    [self.bgScrollView addSubview:addressBtn];
    
    self.bgScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(addressBtn.frame) + 5);
}

#pragma mark - 初始化标题
- (void)setupTitle
{
    self.navigationItem.title = @"优惠详情";
}
#pragma mark - warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--detailsVc");
}
@end
